// ElementManager.cpp : implementation file
//

#include "stdafx.h"
#include "WP3.h"
#include "ElementManager.h"
#include "WP3Doc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CElementManager dialog


CElementManager::CElementManager(CWnd* pParent /*=NULL*/)
	: CDialog(CElementManager::IDD, pParent)
{
	//{{AFX_DATA_INIT(CElementManager)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

	m_pDoc=NULL;
}


void CElementManager::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CElementManager)
	DDX_Control(pDX, IDC_LIST_ELEMENT, m_list);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CElementManager, CDialog)
	//{{AFX_MSG_MAP(CElementManager)
	ON_NOTIFY(NM_CLICK, IDC_LIST_ELEMENT, OnClickListElement)
	ON_BN_CLICKED(IDC_BUTTON_DROPPOS, OnButtonDropPos)
	ON_BN_CLICKED(IDC_BUTTON_CLONE, OnButtonClone)
	ON_BN_CLICKED(IDC_BUTTON_DEL, OnButtonDel)
	ON_BN_CLICKED(IDC_BUTTON_UP, OnButtonUp)
	ON_BN_CLICKED(IDC_BUTTON_DOWN, OnButtonDown)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CElementManager message handlers

BOOL CElementManager::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	TRDLG(IDC_BUTTON_UP,IDS_BUTTONMOVEUP);
	TRDLG(IDC_BUTTON_DOWN,IDS_BUTTONMOVEDOWN);
	TRDLG(IDC_BUTTON_DROPPOS,IDS_BUTTONDROPPOSITION);
	TRDLG(IDC_BUTTON_CLONE,IDS_BUTTONCLONE);
	TRDLG(IDC_BUTTON_DEL,IDS_BUTTONDELETE);


	m_list.InsertColumn(0,TR(IDS_TYPE),LVCFMT_LEFT,260,0);
	m_list.InsertColumn(1,TR(IDS_POSITION),LVCFMT_LEFT,70,1);
	m_list.InsertColumn(2,TR(IDS_SIZE),LVCFMT_LEFT,70,1);
	m_list.ModifyStyle(LVS_TYPEMASK, LVS_REPORT|LVS_SHOWSELALWAYS);
	m_list.SetExtendedStyle( LVS_EX_SUBITEMIMAGES| LVS_EX_ONECLICKACTIVATE|LVS_EX_FULLROWSELECT);
	
	ASSERT(m_pDoc!=NULL);
		
	
	FillElementList();

	EnableButtons(FALSE);	
	



	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CElementManager::FillElementList()
{
	m_list.DeleteAllItems();
	
	POSITION pos;
	CPaintElement* pEl;
	int n;
	CString S;
 	pos = m_pDoc->m_ellist.GetTailPosition();
 	while (pos != NULL)	
	{	pEl=m_pDoc->m_ellist.GetPrev(pos); 
		n=m_list.InsertItem(0,CPaintElement::GetVar("Descr",pEl),0);
		S.Format("%i,%i",pEl->m_pos.left,pEl->m_pos.top);
		m_list.SetItem(n,1,LVIF_TEXT,S,-1,-1,-1,0);
		S.Format("%i,%i",pEl->m_pos.Width(),pEl->m_pos.Height());
		m_list.SetItem(n,2,LVIF_TEXT,S,-1,-1,-1,0);
		m_list.SetItemData(n,(DWORD)pEl);
	}


}

void CElementManager::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

void CElementManager::OnOK() 
{
	// TODO: Add extra validation here
	
	CDialog::OnOK();
}

void CElementManager::OnClickListElement(NMHDR* pNMHDR, LRESULT* pResult) 
{
	POSITION pos = m_list.GetFirstSelectedItemPosition();
	if (pos!= NULL)
	{	EnableButtons(TRUE);
	} else
	{	EnableButtons(FALSE);
	}
	
	*pResult = 0;
}

void CElementManager::EnableButtons(BOOL F)
{
	GetDlgItem(IDC_BUTTON_UP)->EnableWindow(F);
	GetDlgItem(IDC_BUTTON_DOWN)->EnableWindow(F);	
	GetDlgItem(IDC_BUTTON_DROPPOS)->EnableWindow(F);		
	GetDlgItem(IDC_BUTTON_CLONE)->EnableWindow(F);	
	GetDlgItem(IDC_BUTTON_DEL)->EnableWindow(F);
}

void CElementManager::OnButtonDropPos() 
{
	CPaintElement* pEl=GetCurrElement();
	if (pEl==NULL) return;
	pEl->m_pos=CRect(0,0,MIN_SIZE*10,MIN_SIZE*10);
	m_pDoc->UpdateView();
	FillElementList();
	SelectElement(pEl);
}

void CElementManager::OnButtonClone() 
{
	CPaintElement* pEl=GetCurrElement();
	if (pEl==NULL) return;
	ASSERT(m_pDoc!=NULL);
	pEl=m_pDoc->CloneElement(pEl);
	m_pDoc->UpdateView();
	FillElementList();
	SelectElement(pEl);
	
}

void CElementManager::OnButtonDel() 
{
	CPaintElement* pEl=GetCurrElement();
	if (pEl==NULL) return;
	ASSERT(m_pDoc!=NULL);
	m_pDoc->DeleteElement(pEl);
	m_pDoc->UpdateView();
	FillElementList();
	EnableButtons(FALSE);
}

void CElementManager::OnButtonUp() 
{
	CPaintElement* pEl=GetCurrElement();
	if (pEl==NULL) return;
	ASSERT(m_pDoc!=NULL);

	m_pDoc->ChangeZOrder(pEl,TRUE);	

	m_pDoc->UpdateView();
	FillElementList();
	SelectElement(pEl);
	
}

void CElementManager::OnButtonDown() 
{
	CPaintElement* pEl=GetCurrElement();
	if (pEl==NULL) return;
	ASSERT(m_pDoc!=NULL);

	m_pDoc->ChangeZOrder(pEl,FALSE);	

	m_pDoc->UpdateView();
	FillElementList();
	SelectElement(pEl);
	
	
}

CPaintElement* CElementManager::GetCurrElement()
{
	POSITION pos = m_list.GetFirstSelectedItemPosition();
	if (pos==NULL) return NULL;
	int nItem = m_list.GetNextSelectedItem(pos);
	return (CPaintElement*)m_list.GetItemData(nItem);

}

void CElementManager::SelectElement(CPaintElement *pEl)
{
	CPaintElement* pel;
	for (int i=0;i<m_list.GetItemCount();i++) 
	{	pel=(CPaintElement*)m_list.GetItemData(i);
		if (pel==pEl)
		{		m_list.SetItemState(i,LVIS_SELECTED,LVIS_SELECTED);
			return;
		}
	}
}
